/*
 * @lc app=leetcode.cn id=98 lang=golang
 *
 * [98] 验证二叉搜索树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func isValidBST(root *TreeNode) bool {
	return helper(root, -1<<31, 1<<31-1)
}

func helper(root *TreeNode, min int, max int) bool {
	if root == nil {
		return true
	}

	if root.Val > max || root.Val < min {
		return false
	}

	left := helper(root.Left, min, root.Val-1)
	right := helper(root.Right, root.Val+1, max)
	return left && right
}

// @lc code=end

